﻿using MtApi;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MTEA.MTTask
{
    public class MTTaskContext
    {
        public string Symbol { get; }

        public MtApiClient Client
        {
            get
            {
                return client;
            }
        }

        public static readonly string Tag = "MTTaskContext";

        private readonly MtApiClient client;

        public MTTaskContext(string symbol, MtApiClient client)
        {
            this.client = client;
        }

        #region OrderOperation
        public bool SellOrder(double volume)
        {
            int id = Client.OrderSendSell(Symbol, volume, 1);
            if (id != -1)
            {
                return true;
            }
            return false;
        }

        public bool BuyOrder(double volume)
        {
            int id = Client.OrderSendBuy(Symbol, volume, 1);

            if (id != -1)
            {
                //买单成功
                return true;
            }

            return false;
        }

        public bool CloseOrder(int tickId)
        {
            Debug.Assert(tickId != -1);
            var order = Client.GetOrder(tickId, OrderSelectMode.SELECT_BY_TICKET, OrderSelectSource.MODE_TRADES);
            Debug.Assert(order != null);

            var result = Client.OrderClose(tickId, order.Lots, 1);
            return result;

        }

        #endregion

    }
}
